import { Toast } from 'vant'
import { reactive,getCurrentInstance,ref,inject } from 'vue'
import {useRouter} from 'vue-router'
import {BalanceApi} from '@@/api/api.js'

function useWithdrawChannel(){
    const vueState = inject('vueState')
    const {proxy} = getCurrentInstance()
    const router = useRouter()
    const state = reactive({
        channelList:[],
        pageError:false,
    })
    const pageType = ref({
        paytm:'withdraw',
        pix:'withdraw',
        upi:'withdraw',
        flipkart:'giftCard',
        amazon:'giftCard',
        googlePlay:'giftCard',
        phoneRecharge:'phoneRecharge',
        BANK: 'withdraw', //to:langlang  这添加也行
        americanas: "giftCard", //to:langlang
        centauro: "giftCard", //to:langlang
        rewardLink: "giftCard", //to:langlang
    })
    const getData = async()=> {
        Toast.loading({duration:0,message:`${proxy.$t('Loading')}...`})
        try{
            const list = await BalanceApi.WithdrawChannel()
            state.channelList = list
            state.pageError = false
            Toast.clear()
        }catch(e){
            state.pageError = true
            console.log(e);
            setTimeout(()=>{
                Toast.clear()
            },3000)
        }
    }
    const clickChannel = (item)=> {
        window.trackEvent({pageTitle:'withdrawpage',ev:'clickPaymentMethod',remarks:`Type=${item.type}`})
        // if(item.type === 'paypal' || item.type === 'bankCard' || item.type === 'upi'){
        //     Toast(proxy.$t('Paying and docking, so stay tuned'))
        //     return
        // }
        if(!item.effectiveChannel){
            Toast(proxy.$t('The inventory is full today'))
            return
        }
        goPage(item)
        vueState.pageInfo.withdrawChannel = item
    }
    const goPage = (item)=> {
        // console.log(item);
        const {type} = item
        const channel = JSON.stringify(item)
        switch (pageType.value[type]) {
            case 'giftCard':
                router.push({name:'giftCard',params:{
                    channel
                }})
                break;
            case 'phoneRecharge':
                router.push({name:'recharge',params:{
                    channel
                }})
                break;
            case 'withdraw':
                router.push({name:'withdrawDetail',params:{
                    channel
                }})
                break;
            default:
                break;
        }
    }
    return {
        state,
        getData,
        clickChannel,
    }
}

export {useWithdrawChannel}